package org.loy.eclipse.post_debug_perspective;

import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchesListener2;
import org.eclipse.jface.preference.IPreferenceStore;
import org.loy.eclipse.post_debug_perspective.preferences.PreferenceConstants;

public class LaunchesListener implements ILaunchesListener2
{
	@Override
	public void launchesRemoved(ILaunch[] launches)
	{
	}

	@Override
	public void launchesAdded(ILaunch[] launches)
	{
	}

	@Override
	public void launchesChanged(ILaunch[] launches)
	{
	}

	@Override
	public void launchesTerminated(ILaunch[] launches)
	{
		// lazy preferences store initialization
		if (store == null) {
			store = Activator.getDefault().getPreferenceStore();
		}
		
		for (ILaunch launch : launches) {
			if (!launch.getLaunchMode().equals("debug")) {
				continue ;
			}

			// switch to perspective
			String perspective = store.getString(PreferenceConstants.P_PERSPECTIVE_TO_SET);
			if (perspective.equals(PreferenceConstants.DO_NOT_SWITCH_PERSPECTIVE)) {
				continue ;
			}

			// TODO check is perspective currently available
			
			SetPerspectiveJob job = new SetPerspectiveJob(perspective);
			job.schedule();
		}
	}

	private IPreferenceStore store;
}
